package com.qf.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.page.PageInfo;
import com.qf.common.page.PageUtils;
import com.qf.common.response.Response;
import com.qf.entity.dto.Merchant;
import com.qf.service.MerchantService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("merchant")
public class MerchantController {

    @Resource
    MerchantService merchantService;

    /**
     * 将商户信息集合返回给前端
     *
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String toMerchantList(Model model, @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage) {
        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(Merchant::getCreateTime);
        PageInfo<Merchant> pageInfo = PageUtils.getPageList(currentPage, wrapper, merchantService);
        model.addAttribute("merchantList", pageInfo.getPageRecords());
        model.addAttribute("pageCounts",pageInfo.getPageCounts() );
        return "merchant/list";
    }


    /**
     * 根据前端数据对merchant表进行添加或修改记录
     *
     * @param merchant
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public Response<?> addOrUpdateMerchant(Merchant merchant) {
        merchantService.saveOrUpdate(merchant);
        return Response.success();
    }

    /**
     * 根据商户id查询该商户信息
     *
     * @param merchantId
     * @return
     */
    @RequestMapping("getById")
    @ResponseBody
    public Response<?> getMerchantById(@RequestParam("id") Long merchantId) {
        return Response.success(merchantService.getById(merchantId));
    }

    /**
     * 根据商户id对merchant表进行删除记录
     *
     * @param merchantId
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public Response<?> addOrUpdateMerchant(@RequestParam("id") Long merchantId) {
        merchantService.removeById(merchantId);
        return Response.success();
    }
}
